package com.tulingxueyuan.mall.controller;

import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.ums.model.UmsMember;
import com.tulingxueyuan.mall.modules.ums.model.UmsMemberReceiveAddress;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberReceiveAddressService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member/address")
@Api(tags = "MemberAddressController" ,description = "收获地址的新增、删除、修改")
public class MemberAddressController {

    @Autowired
    UmsMemberReceiveAddressService addressService;



    /**
     * 收获地址的新增
     *  (method = "post"), (url = "/member/address/add");
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(@RequestBody UmsMemberReceiveAddress address) {


       boolean result = addressService.add(address);
       if (result) {
           return CommonResult.success(result);
       } else {
           return CommonResult.failed();
       }
    }

    /**
     * 收获地址的修改
     *  (method = "post"), (url = `/member/address/update/${checkedItem.id}`);
     * @param address
     * @return
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult edit(@RequestBody UmsMemberReceiveAddress address,@PathVariable Long id) {

        boolean result = addressService.edit(address,id);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除收货地址
     * (method = "post"), (url = `/member/address/delete/${checkedItem.id}`);
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {

        boolean result = addressService.delete(id);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取收货地址列表
     * this.axios.get("/member/address/list").then((res) => {
     *         this.list = res;
     *       });
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list() {
        List<UmsMemberReceiveAddress> addressList = addressService.listByMemberId();


        return CommonResult.success(addressList);
    }
}
